package xyz.labmem.lipx.core.console.enums


import xyz.labmem.lipx.core.context.ServerContext.userList
import xyz.labmem.lipx.core.AppContext
import xyz.labmem.lipx.core.tool.ConsoleTable
import xyz.labmem.lipx.core.util.ServerConfigDataUtil

/**
 * @description: do something
 * @author: ltty
 * @date: 2022/8/9 11:41
 */
enum class DisplayServer : DisplayEnum {

    CONFIG,//配置
    STATUS;//服务端端状态

    override fun getName(): String = "server/${this.name}"

    /**
     * 展示内容
     */
    override fun content(): String {
        return when (this) {

            STATUS -> {
                val title = """
                当前服务状态：${if (AppContext.Server.run()) "【前进四!】启动时间：${AppContext.Server.startDate}" else "【面壁中】"}
                连接数量(${userList.size})""".trimIndent()
                return title +"\n"+ ConsoleTable.create()
                    .addHeader("连接时间", "连接IP", "连接名称", "使用端口", "代理模式")
                    .apply {
                        userList.forEach {
                            addBody(
                                it.value.connectTime ?: "",
                                it.value.ip ?: "",
                                it.value.name ?: "",
                                (it.value.port ?: "").toString(),
                                if (it.value.isReceive) "正向代理" else "反向代理"
                            )
                        }
                    }.toString()

            }

            CONFIG -> """
                本地配置文件：【${ServerConfigDataUtil.sap}/lipxData.json】
                [port] 服务端口: ${AppContext.Server.cacheData?.port ?: ""}
                [pwd] 密码: ${AppContext.Server.cacheData?.password ?: ""}
                [wls] 白名单IP: ${AppContext.Server.cacheData?.wls.toString()}
                [auto] 自启服务: ${AppContext.Server.cacheData?.autoRun.toString()}
            """.trimIndent()
        }
    }

    /**
     * 操作提示
     */
    override fun hint(): String {
        return "tips: " + when (this) {
            CONFIG -> "[edit 'key' 'val']编辑 [save]保存 [back]返回"
            STATUS -> "[config]配置 [start]启动服务 [close]关闭服务 [r]刷新 [back]返回"
        }
    }

    override fun keys(): List<String> {
        return when (this) {
            CONFIG -> listOf("edit", "save", "back", "r", "./")
            STATUS -> listOf("config", "c", "start", "close", "r", "back", "./")
        }
    }
}